package com.kindnesslibrary.network;

import com.kindnesslibrary.network.callback.BaseCallBack;
import com.kindnesslibrary.network.get.GetFormThread;
import com.kindnesslibrary.network.post.PostFormThread;
import com.kindnesslibrary.network.upload.SingleUploadFileThread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * Http管理者
 */
public class HttpManager {

    private List<BaseCallBack> callbackList = new ArrayList<>();// get、post请求回调集合
    private Executor executor = HttpConfig.getInstance().getExecutor(); // 线程池

    private static HttpManager httpManager; // 单例

    /**
     * 私有构造
     */
    private HttpManager(){

    }

    public static HttpManager getInstance(){
        if(httpManager == null){
            synchronized (HttpManager.class){
                if(httpManager == null){
                    httpManager = new HttpManager();
                }
            }
        }
        return httpManager;
    }

    /**
     * 添加Post请求线程
     * @param postFormThread Post线程
     * @return
     */
    public boolean addPostFormCallBack(PostFormThread postFormThread){
        if(postFormThread == null){
            throw new NullPointerException("请求Callback不可为Null");
        }
        if (callbackList.indexOf(postFormThread) < 0) {
            postFormThread.isRemove = false;
            callbackList.add(postFormThread);
            executor.execute(postFormThread);
            return true;
        }
        return false;
    }

    /**
     * 添加Get请求线程
     * @param getFormThread Post线程
     * @return
     */
    public boolean addGetFormCallBack(GetFormThread getFormThread){
        if(getFormThread == null){
            throw new NullPointerException("请求Callback不可为Null");
        }
        if (callbackList.indexOf(getFormThread) < 0) {
            getFormThread.isRemove = false;
            callbackList.add(getFormThread);
            executor.execute(getFormThread);
            return true;
        }
        return false;
    }

    /**
     * 添加文件上传回调
     * @param singleUFThread 单文件上传线程
     * @return
     */
    public boolean addSingleFUCallBack(SingleUploadFileThread singleUFThread){
        if(singleUFThread == null){
            throw new NullPointerException("请求Callback不可为Null");
        }
        if (callbackList.indexOf(singleUFThread) < 0) {
            singleUFThread.isRemove = false;
            callbackList.add(singleUFThread);
            executor.execute(singleUFThread);
            return true;
        }
        return false;
    }

    /**
     * 请求方式
     */
    public enum  RequestWay{
        POST, GET
    }

}
